<script setup lang="ts">
import KnowledgeCard from './KnowledgeCard.vue'
import { getKnowledgePage } from '../../../services/consult'
import { KnowledgeType, KnowledgeList } from '../../../types/consult'
import { ref } from 'vue'
const props = defineProps<{
  type: KnowledgeType
}>()

const list = ref<KnowledgeList[]>([])
const pageParams = ref<KnowledgeParams>({
  current: 1,
  pageSize: 10,
  type: props.type
})
const loading = ref(false)
const finished = ref(false)
const onLoad = async () => {
  // 加载数据
  const res = await getKnowledgePage(pageParams.value)
  // 将数据追加到数组中
  list.value.push(...res.data.rows)
  if (pageParams.value.current <= res.data.pageTotal) {
    pageParams.value.current++
  } else {
    finished.value = true
  }
  // 加载状态结束
  loading.value = false
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <knowledge-card v-for="item in list" :key="item.id" :item="item" />
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
